﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProjectEulerSolutions
{
    class Problem17
    {
        public static string Calculate()
        {
            string result = " ";

            List<string> words = new List<string>();

            for (int i = 1; i <= 1000; i++)
            {
                Console.WriteLine(ConvertToWords(i));
                words.Add(ConvertToWords(i));
            }

            long sum = words.Sum(x => x.Count(y => char.IsLetter(y)));

            result = sum.ToString();

            return result;
        }


        public static String ConvertToWords(long number)
        {

            string stringVersion = number.ToString();

            string result = "";

            for (int i = 0; i < stringVersion.Length; i++)
            {
                int positionInString = stringVersion.Length - 1 - i;
                int digit = stringVersion[i] - '0';

                //prvi brojevi
                if (positionInString % 3 == 0 || positionInString % 3 == 2)
                {
                    result += " " + keys[digit];
                }
                else if (positionInString % 3 == 1)
                {
                    if (digit == 0)
                    {

                    }
                    else if (digit == 1)
                    {
                        i++;
                        int digit2 = stringVersion[i] - '0';
                        positionInString = stringVersion.Length - 1 - i;
                        result += " " + keys[digit * 10 + digit2];
                    }
                    else
                    {
                        result += " " + keys[digit * 10];
                    }
                }

                if (positionInString % 3 == 2)
                {
                    if (digit != 0)
                        result += " " + keys[100];
                }

                if (positionInString == 2)
                {
                    if (stringVersion[stringVersion.Length - 2] != '0' || stringVersion[stringVersion.Length - 1] != '0')
                        result += " and";
                }

                if (positionInString == 3)
                    result += " " + keys[1000];

                if (positionInString == 6)
                    result += " " + keys[1000000];

                if (positionInString == 9)
                    result += " " + keys[1000000000];
            }

            return result;
        }

        static Dictionary<long, string> keys = new Dictionary<long, string>()
        {
            {0,""},
            {1,"one"},
            {2,"two"},
            {3,"three"},
            {4,"four"},
            {5,"five"},
            {6,"six"},
            {7,"seven"},
            {8,"eight"},
            {9,"nine"},
            {10,"ten"},
            {11,"eleven"},
            {12,"twelve"},
            {13,"thirteen"},
            {14,"fourteen"},
            {15,"fifteen"},
            {16,"sixteen"},
            {17,"seventeen"},
            {18,"eighteen"},
            {19,"nineteen"},
            {20,"twenty"},
            {30,"thirty"},
            {40,"forty"},
            {50,"fifty"},
            {60,"sixty"},
            {70,"seventy"},
            {80,"eighty"},
            {90,"ninety"},
            {100,"hundred"},
            {1000,"thousand"},
            {1000000, "million"},
            {1000000000, "billion"},
            {100000000000000, "bajillion"}
        };
    }
}
